package net.siufung.simple.config;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import net.siufung.boot.executor.utils.ThreadMdcUtil;
import net.siufung.mybatis.properties.MybatisProperties;
import net.siufung.security.api.service.IAccessTokenService;
import net.siufung.simple.config.security.interceptor.SecurityHandlerInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 陈建峰
 * @since 2022/5/5 5:33 下午
 */
@Configuration
@AllArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final IAccessTokenService accessTokenService;
    private final MybatisProperties mybatisProperties;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(securityHandlerInterceptor());
        // registry.addInterceptor(idempotentInterceptor());
        registry.addInterceptor(new HandlerInterceptor(){
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
                String tid = IdUtil.fastSimpleUUID();
                if (StrUtil.isNotBlank(request.getHeader(ThreadMdcUtil.TRACE_ID))){
                    tid = request.getHeader(ThreadMdcUtil.TRACE_ID);
                }
                MDC.put(ThreadMdcUtil.TRACE_ID, tid);
                return Boolean.TRUE;
            }

            @Override
            public void afterCompletion(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Object handler, Exception ex) throws Exception {
                MDC.remove(ThreadMdcUtil.TRACE_ID);
            }
        });
    }

    @Bean
    public HandlerInterceptor securityHandlerInterceptor() {
        return new SecurityHandlerInterceptor(accessTokenService, mybatisProperties);
    }

//    @Bean
//    public HandlerInterceptor idempotentInterceptor(){
//        return new IdempotentInterceptor(redisClientService);
//    }
}
